<?php
class IndexAction extends Action {
    function index() {
    	if((empty($_GET['model']) && empty($_GET['service'])) || empty($_GET['method'])) {
    		return ;
    	}
    	
    	if(!empty($_GET['service'])) {
	    	$service	=	X($_GET['service']);
			$service->$_GET['method']($this->fill_params($_GET['params']));
			//实例化服务
	    	//eval('$service = service(' . $_GET['service'] . ');');
	    	//组装类方法
	    	//$str = '$service->' . $_GET['method'] . '(' . $this->fill_params($_GET['params']) . ');';
			//执行服务
	    	//eval($str);
			echo "service:".$_GET['service']."-ok";
    	}elseif(!empty($_GET['model'])) {
    		if(empty($_GET['appl'])) return ;
    		//使用D函数执行
    		$model	=	D($_GET['model'],$_GET['appl'])->$_GET['method']($this->fill_params($_GET['params']));
			//$str = 'D(\'' . $_GET['model'] . '\', \'' . $_GET['appl'] . '\')->' . $_GET['method'] . '(' . $this->fill_params($_GET['params']) . ');';
			//eval($str);
			echo "model:".$_GET['service']."-ok";
    	}
    }
    
    function test() {
        dump('^_^');
        //模拟其他应用调用api时的跳转
        redirect(U('api/Index/index','service=DomainAuth&method=test&params[0][task_to_run]=xxxxx/index.php?test=test&params[0][schedule_type]=MONTHLY&params[0][start_datetime]=ccc&params[1]=ddd'));
//        redirect(U('api/Index/index','model=space&appl=home&method=test&params[0][task_to_run]=xxxxx/index.php?test=test&params[0][schedule_type]=MONTHLY&params[0][start_datetime]=ccc&params[1]=ddd'));
    }
    
    //组装参数
    function fill_params($params = '') {
   		$result = '';
    	if(is_array($params)) {
    		$flag = true;
	    	foreach ($params as $k => $v) {
	    		if($flag == true) {
	    			$result = $result . $this->format_params($v);
	    			$flag = false;
	    		}else {
	    			$result = $result . ',' . $this->format_params($v);
	    		}
	    	}
    	}else {
    		$result = $params;
    	}
    	return $result;
    }
    
    //格式化参数
    function format_params($params) {
    	if(is_array($params)) {
	    	$result = 'Array(';
	    	foreach ($params as $k => $v) {
	    		$result = $result . "'$k'=>'$v',";
	    	}
	    	$result .= ')';
	    	return $result;
    	}else {
    		return '\'' . $params . '\'';
    	}
    }
}
?>